package com.yzc.util;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Slf4j
@Component
@RequiredArgsConstructor
public class RabbitMQUtil {

    private final RabbitTemplate rabbitTemplate;

    /**
     * 发送阶梯式延迟消息
     *
     * @param exchange   交换机名称
     * @param routingKey 路由键
     * @param message    消息内容
     */
    public void sendStaggeredDelayedMessages(String exchange, String routingKey, Object message) {
        int delay = 30 * 60 * 1000; // 30 分钟
        rabbitTemplate.convertAndSend(
                exchange,
                routingKey,
                message,

                msg -> {
                    // 设置延迟时间
                    msg.getMessageProperties().setDelay(delay);
                    return msg;
                }

        );
        log.info(" [x] 将延迟消息发送到交换 {}, 路由键: {}, 延迟: {} ms", exchange, routingKey, delay);
    }

    /**
     * 动态生成阶梯式延迟时间
     *
     * @return 阶梯式延迟时间数组
     */


    public void cancelDelayedMessage(String exchange, String routingKey, Long orderId) {
        // 发送一条取消消息到相同的队列
        rabbitTemplate.convertAndSend(
                exchange,
                routingKey,
                "CANCEL:" + orderId,
                msg -> {
                    // 设置消息的唯一 ID
                    msg.getMessageProperties().setMessageId("CANCEL:" + orderId);
                    // 设置延迟时间为 0（立即发送）
                    msg.getMessageProperties().setDelay(0);
                    return msg;
                }
        );
        log.info(" [x] 发送取消消息到交换 {}, 路由键: {}, 订单ID: {}", exchange, routingKey, orderId);
    }
}